package com.cbs.android.module.shit;

import android.os.Handler;

import com.cbs.android.component.applicationutils.Global;
import com.cbs.android.component.log.L;
import com.cbs.android.component.network.HttpClient;
import com.cbs.android.component.network.Request;
import com.cbs.android.component.network.Response;
import com.cbs.android.component.network.decoder.GsonDecoder;
import com.cbs.android.module.user.network.CBSUserRequest;
import com.cbs.android.module.user.user.UserModule;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;
import java.util.Vector;

/**
 * Created by broche on 7/14/16.
 */
public class ShitThread extends Thread {
    private static final String ShitUrl = "%s/shit/api/v1/users/{uid}/shits";
    private static final String DeleteShitUrl = "%s/shit/api/v1/shits/%d";
    private static final String TAG = ShitThread.class.getName();
    private HttpClient httpClient;
    private Vector<Shit> shitList;
    private Handler shitHandler;

    public ShitThread(Handler shitHandler) {
        httpClient = Global.getHttpClient().clone();
        httpClient.setDebug(ShitModule.isDebug());
        shitList = new Vector<>();
        this.shitHandler = shitHandler;
    }

    public Shit getShit(int shitType, int subjectId) {
        if (UserModule.getUser() != null) {
            int userId = UserModule.getUser().getId();
            for (Shit shit : shitList) {
                if (shit.getUserid() == userId && shit.getShittype() == shitType && shit.getSubjectid() == subjectId) {
                    return shit;
                }
            }
            return null;
        } else {
            return null;
        }
    }

    public void deleteShit(int shitType, int subjectId) {
        if (UserModule.getUser() != null) {
            Shit shit = getShit(shitType, subjectId);
            if (shit != null) {
                Request request = new CBSUserRequest(String.format(DeleteShitUrl, Global.getServerHost(), shit.getId()))
                        .delete();
                httpClient.send(request);
                shitList.remove(shit);
            }
        }
    }

    @Override
    public void run() {
        try {
            while (true) {
                try {
                    if (UserModule.getUser() != null) {
                        Request request = new CBSUserRequest(String.format(ShitUrl, Global.getServerHost()))
                                .setDecoder(new GsonDecoder(new TypeToken<List<Shit>>() {
                                }.getType()));
                        Response response = httpClient.sendSync(request);
                        if (response.isSuccessful()) {
                            List<Shit> shits = (List<Shit>) response.getResult();
                            shitList.clear();
                            shitList.addAll(shits);
                            shitHandler.obtainMessage().sendToTarget();
                        }
                    } else {
                        shitList.clear();
                        shitHandler.obtainMessage().sendToTarget();
                    }

                    sleep(ShitModule.getInterval());
                } catch (IOException e) {
                    L.e(TAG, "", e);
                }
            }
        } catch (InterruptedException e) {
            ShitThread.this.interrupt();
            // do nothing
        }
    }
}
